//
//  MBOrderSectionFooterView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBOrderSectionFooterView.h"

@interface MBOrderSectionFooterView ()
@property (nonatomic,weak)UIButton* typeButton;
@property (nonatomic,weak)UILabel* timeLabel;
@property (nonatomic,weak)CAGradientLayer* gradientLayer0;

@end

@implementation MBOrderSectionFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UILabel* timeLabel = [[UILabel alloc]init];
        self.timeLabel = timeLabel;
        timeLabel.text = @"下单时间：2018-04-07";
        timeLabel.font = [UIFont lf_systemFontOfSize:13];
        timeLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
        [self addSubview:timeLabel];
        
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset([UIView lf_sizeFromIphone6:14]);
            make.right.equalTo(self).offset([UIView lf_sizeFromIphone6:-12]);
        }];
        
        UIButton* typeButton = [[UIButton alloc]init];
        self.typeButton = typeButton;
        typeButton.layer.borderColor = [UIColor colorWithR:0 G:123 B:46].CGColor;
        typeButton.layer.borderWidth = [UIView lf_sizeFromIphone6:1];
        typeButton.layer.cornerRadius = [UIView lf_sizeFromIphone6:4];
        typeButton.layer.masksToBounds = YES;
        [typeButton setTitle:@"待成团" forState:UIControlStateNormal];
        typeButton.titleLabel.font = [UIFont lf_systemFontOfSize:11];
        [typeButton setTitleColor:[UIColor colorWithR:0 G:123 B:46] forState:UIControlStateNormal];
        [self addSubview:typeButton];
        
        [typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(timeLabel);
            make.left.equalTo(self).offset([UIView lf_sizeFromIphone6:13]);
            make.width.mas_equalTo(typeButton.intrinsicContentSize.width+[UIView lf_sizeFromIphone6:5]);
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:16]);
        }];


        UIView* line = [[UIView alloc]init];
        line.backgroundColor = [UIColor colorWithR:242 G:242 B:242];
        [self addSubview:line];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(timeLabel.mas_bottom).offset([UIView lf_sizeFromIphone6:14]);
            make.left.equalTo(self).offset([UIView lf_sizeFromIphone6:12]);
            make.right.equalTo(self).offset([UIView lf_sizeFromIphone6:-12]);
            make.height.mas_equalTo([UIView lf_sizeFromIphone6:1]);
        }];
        
        UIButton* shureButton = [[UIButton alloc]init];
        self.shureButton = shureButton;
        shureButton.layer.cornerRadius = [UIView lf_sizeFromIphone6:25/2];
        shureButton.layer.masksToBounds = YES;
        //Gradient 0 fill for 圆角矩形 1 拷贝
        CAGradientLayer *gradientLayer0 = [[CAGradientLayer alloc] init];
        self.gradientLayer0 = gradientLayer0;
        gradientLayer0.frame = CGRectMake(0, 0, [UIView lf_sizeFromIphone6:58], [UIView lf_sizeFromIphone6:25]);
        gradientLayer0.colors =@[
                                 (id)[UIColor colorWithRed:255.0f/255.0f green:99.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                                 (id)[UIColor colorWithRed:255.0f/255.0f green:136.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor];
        gradientLayer0.locations = @[@0, @1];
        [gradientLayer0 setStartPoint:CGPointMake(0, 0)];
        [gradientLayer0 setEndPoint:CGPointMake(1, 1)];
        [shureButton.layer addSublayer:gradientLayer0];
        [shureButton setTitle:@"确认" forState:UIControlStateNormal];
        shureButton.titleLabel.font = [UIFont lf_systemFontOfSize:12];
        [self addSubview:shureButton];
        
        [shureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset([UIView lf_sizeFromIphone6:-12]);
            make.top.equalTo(line.mas_bottom).offset([UIView lf_sizeFromIphone6:8]);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:58], [UIView lf_sizeFromIphone6:25]));
        }];

    }
    return self;
}

-(void)setViewModel:(MBOrderTableCellViewModel *)viewModel
{
    _viewModel = viewModel;
    
    if (viewModel.dataModel.isGroup) {
        self.typeButton.hidden = NO;
        if (viewModel.dataModel.status == 1) {
            [self.typeButton setTitle:@"待成团" forState:UIControlStateNormal];
        }else if (viewModel.dataModel.status == 2){
            [self.typeButton setTitle:@"已成团" forState:UIControlStateNormal];
        }else if(viewModel.dataModel.status == 3){
            [self.typeButton setTitle:@"未成团" forState:UIControlStateNormal];
        }else {
            self.typeButton.hidden = YES;
        }
    }else {
        self.typeButton.hidden = YES;
    }
    self.timeLabel.text = [NSString stringWithFormat:@"下单时间: %@",viewModel.dataModel.add_time];
    
    if (viewModel.dataModel.pay_status == 1) {
        if (viewModel.dataModel.order_status == 0 || viewModel.dataModel.order_status == 1) {
            self.shureButton.userInteractionEnabled = YES;
            self.gradientLayer0.hidden = NO;
            [self.shureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            if (viewModel.dataModel.order_status == 1) {
                if (viewModel.dataModel.shipping_status == 0) {
                    [self.shureButton setTitle:@"发货" forState:UIControlStateNormal];
                }else if(viewModel.dataModel.shipping_status == 1){
                    self.shureButton.userInteractionEnabled = NO;
                    self.gradientLayer0.hidden = YES;
                    [self.shureButton setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
                    [self.shureButton setTitle:@"已发货" forState:UIControlStateNormal];
                }else {
                    [self.shureButton setTitle:@"部分发货" forState:UIControlStateNormal];
                }
            }else {
                [self.shureButton setTitle:@"确认" forState:UIControlStateNormal];
            }
        }else {
            self.gradientLayer0.hidden = YES;
            self.shureButton.userInteractionEnabled = NO;
            [self.shureButton setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
            
            if (viewModel.dataModel.order_status == 2) {
                [self.shureButton setTitle:@"已收货" forState:UIControlStateNormal];
            }else if (viewModel.dataModel.order_status == 3){
                [self.shureButton setTitle:@"已取消" forState:UIControlStateNormal];
            }else if (viewModel.dataModel.order_status == 4){
                [self.shureButton setTitle:@"已完成" forState:UIControlStateNormal];
            }else {
                [self.shureButton setTitle:@"已作废" forState:UIControlStateNormal];
            }
        }
    }else {
//        未支付
        [self.shureButton setTitleColor:[UIColor colorWithR:24 G:24 B:24] forState:UIControlStateNormal];
        self.gradientLayer0.hidden = YES;
        [self.shureButton setTitle:@"待支付" forState:UIControlStateNormal];
        self.shureButton.userInteractionEnabled = NO;
    }
}

@end
